import pygame
from types import SimpleNamespace
from common import (
    white,
    black,
    red,
    green,
    blue,
    font_path,
    pink,
    go_next_level,
    get_score,
    add_score,
)

screen_width, screen_height = 1000, 700
circuit_image_pre = pygame.image.load("./assets/circuit_1-2-pre.png")
circuit_image_post = pygame.image.load("./assets/circuit_1-3-post.png")
circuit_image = circuit_image_pre
button_text_font = pygame.font.Font(font_path, 20)
title_text_font = pygame.font.Font(font_path, 30)

button_k1 = SimpleNamespace(
    rect=pygame.Rect(250, 400, 150, 50),
    color=pink,
    text="K1按下",
    font_size=20,
    font_color=black,
)
button_k2 = SimpleNamespace(
    rect=pygame.Rect(250, 500, 150, 50),
    color=pink,
    text="K2按下",
    font_size=20,
    font_color=black,
)
button_k3 = SimpleNamespace(
    rect=pygame.Rect(250, 600, 150, 50),
    color=pink,
    text="K3按下",
    font_size=20,
    font_color=black,
)
button_ensure = SimpleNamespace(
    rect=pygame.Rect(600, 600, 100, 50),
    color=blue,
    text="确定",
    font_size=20,
    font_color=black,
)
button_submit = SimpleNamespace(
    rect=pygame.Rect(600, 500, 100, 50),
    color=blue,
    text="提交",
    font_size=20,
    font_color=black,
)


buttons = [
    button_k1,
    button_k2,
    button_k3,
    button_ensure,
    button_submit,
]

text_notification = SimpleNamespace(
    rect=pygame.Rect(600, 400, 150, 50),
    text="操作错误，提交进入下一关",
    font_color=red,
)

k1_selected = -1
k2_selected = -1
k3_selected = -1
ensure = False
score = 0


def draw(screen):
    global selected, score, circuit_image, k1_selected, k2_selected, k3_selected, ensure
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pass
        elif event.type == pygame.MOUSEBUTTONUP:
            for button in buttons:
                if button.rect.collidepoint(event.pos):
                    index = buttons.index(button)
                    # 吸起是0， 打落是1
                    if index == 0 and k1_selected != 1 and not ensure:
                        button.text = "k1按下(已选择)"
                        k1_selected = 1
                    elif index == 0 and k1_selected == 1 and not ensure:
                        button.text = "k1按下"
                        k1_selected = 0
                    elif index == 1 and k2_selected != 1 and not ensure:
                        k2_selected = 1
                        button.text = "k2按下(已选择)"
                    elif index == 1 and k2_selected == 1 and not ensure:
                        k2_selected = 0
                        button.text = "k2按下"
                    elif index == 2 and k3_selected != 1 and not ensure:
                        k3_selected = 1
                        button.text = "k3按下(已选择)"
                    elif index == 2 and k3_selected == 1 and not ensure:
                        k3_selected = 0
                        button.text = "k3按下"
                    elif index == 3:
                        ensure = True
                        if k3_selected != 1 and k2_selected == 1 and k1_selected != 1:
                            score = 10
                            add_score(score)
                            circuit_image = circuit_image_post
                        else:
                            score = -10
                            add_score(score)
                    elif index == 4 and ensure:
                        go_next_level()

    # 绘制背景
    screen.fill(white)

    # 绘制title
    font = title_text_font
    text = font.render(
        f"当前得分（{get_score()}）关卡1-3：正确操作使D2亮灯",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 50))
    screen.blit(text, text_rect)

    # 绘制电路板
    circuit = pygame.transform.smoothscale(circuit_image, (1000, 250))
    screen.blit(circuit, (0, 100))

    if score == -10:
        font = button_text_font
        text_no = font.render(
            text_notification.text, True, text_notification.font_color
        )
        text_no_rect = text_no.get_rect(center=text_notification.rect.center)
        screen.blit(text_no, text_no_rect)

    for _, button in enumerate(buttons):
        font = button_text_font
        text = font.render(button.text, True, button.font_color)
        text_rect = text.get_rect(center=button.rect.center)
        # 绘制按钮
        pygame.draw.rect(screen, button.color, button.rect)
        screen.blit(text, text_rect)
        # 绘制文字
        screen.blit(text, text_rect)
